﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Linq;
using StockholmsStad.Data.Entity;

namespace StockholmsStad.Data
{
    /// <summary>
    /// Implementation av interfacet IDataContextGeneric
    /// </summary>
    internal class DataContextQueries : IDataContextQueries
    {

        public List<CeremonyLanguageView> GetAllCeremonyLanguages()
        {
            var db = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            IQueryable<CeremonyLanguageView> query = (from l in db.CeremonyLanguageViews
                                                      select l);
            List<CeremonyLanguageView> list = query.ToList();
            return list;
        }

        public List<CertificateLanguageView> GetAllCertificateLanguages()
        {
            var db = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            IQueryable<CertificateLanguageView> query = (from l in db.CertificateLanguageViews
                                                         select l);
            List<CertificateLanguageView> list = query.ToList();
            return list;
        }

        public List<SpecialNeedView> GetAllSpecialNeeds()
        {
            var db = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            IQueryable<SpecialNeedView> query = (from l in db.SpecialNeedViews
                                                         select l);
            List<SpecialNeedView> list = query.ToList();
            return list;
        }


        public CoupleView GetCoupleById(int coupleId)
        {
            var db = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            CoupleView returnValue = (from c in db.CoupleViews
                                      where c.CoupleId == coupleId
                                      select c).SingleOrDefault();
            return returnValue;
        }

        public CeremonyView GetCeremonyById(int ceremonyId)
        {
            var db = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            CeremonyView returnValue = (from c in db.CeremonyViews
                                        where c.CeremonyId == ceremonyId
                                        select c).SingleOrDefault();
            return returnValue;
        }

        public PersonView GetPersonById(int personId)
        {
            var db = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            PersonView returnValue = (from c in db.PersonViews
                                      where c.PersonId == personId
                                      select c).SingleOrDefault();
            return returnValue;
        }

        /// <summary>
        /// Hämtar data ur en tabell då man vet att kolumnen bara ska
        /// motsvara en rad. Kolumnen bör bara definierad som unique i databasen.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columnName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private T GetByUniqueColumnKey<T>(string columnName, string value) where T : class
        {
            var dbCtx = new StockholmsStadDataContext(IsolationLevel.RepeatableRead);
            // hämta tabellen för typen T
            Table<T> table = dbCtx.GetTable<T>();

            // hitta primärnyckeln
            // by checking for IsPrimaryKey
            string primaryKey = columnName;

            // hämta aktuellt objekt 
            return table.SingleOrDefault(
                delegate(T t)
                    {
                        String memberId =
                            t.GetType().GetProperty(primaryKey).GetValue(t, null).
                                ToString();
                        return memberId == value;
                    });
        }
    }
}